package com.pro_one.controller;


import com.github.pagehelper.PageInfo;
import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.exception.CheckItemDeleteFailException;
import com.pro_one.pojo.CheckItem;
import com.pro_one.service.CheckItemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.addCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 列表查询以及根据条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByCondition")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = checkItemService.findByCondition(queryPageBean);
        return pageResult;

    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            System.out.println(1234);
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除检查项，如果关联检查组，不允许删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id) {
        try {
            checkItemService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            if (e instanceof CheckItemDeleteFailException) {
                return new Result(false, "删除失败,检查项与检查组存在关联，请先删除检查组或联系管理员");
            }
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

}
